package com.person.yan.a03_takeout1.view;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.person.yan.a03_takeout1.R;
import com.person.yan.a03_takeout1.bean.Goods;
import com.person.yan.a03_takeout1.contract.ListContract;
import com.person.yan.a03_takeout1.dagger.component.DaggerMainActivityComponent;
import com.person.yan.a03_takeout1.dagger.component.MainActivityComponent;
import com.person.yan.a03_takeout1.dagger.module.MainActivityMoudle;
import com.person.yan.a03_takeout1.presenter.ListPresenter;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity implements ListContract.View {

    @BindView(R.id.rv)
    RecyclerView rv;
    @BindView(R.id.pbar)
    ProgressBar pbar;
    private MainListAdapter adapter;
    private Activity mActivity;
    @Inject
    ListPresenter mPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        DaggerMainActivityComponent.builder()
                .mainActivityMoudle(new MainActivityMoudle(this,this))
                .build()
                .injectTarget(this);
        ButterKnife.bind(this);
        mActivity = this;
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        rv.setLayoutManager(manager);

//        mPresenter = new ListPresenter(this,this);
        mPresenter.loadData();

    }

    @Override
    public void showLoading(boolean show) {
        pbar.setVisibility(show ? View.VISIBLE : View.GONE);
    }

    @Override
    public void showRv(List<Goods> list) {
        showLoading(false);
        adapter = new MainListAdapter(list);
        rv.setAdapter(adapter);

    }

    @Override
    public void refreashRv() {
        adapter.notifyDataSetChanged();
    }

    private class MainListAdapter extends RecyclerView.Adapter<ViewHolder>{

        private List<Goods> mData;

        public MainListAdapter(List<Goods> mData) {

            this.mData = mData;
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

            int id = R.layout.item_outteradapter;
            View inflate = LayoutInflater.from(mActivity).inflate(id, parent , false);
            ViewHolder holder = new ViewHolder(inflate);
            return holder;
        }

        @Override
        public void onBindViewHolder(final ViewHolder holder, final int position) {
            Goods goods = mData.get(position);
            holder.tvOutterAdapterText.setText(goods.getDesc());
            holder.tvOutterAdapterAdd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    mPresenter.addItem(getItemCount());
                }
            });

            holder.tvOutterAdapterDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mPresenter.deleteItem(position);
                }
            });
        }

        @Override
        public int getItemCount() {
            return mData.size();
        }
    }


    static class ViewHolder extends RecyclerView.ViewHolder{
        @BindView(R.id.tv_outter_adapter_text)
        TextView tvOutterAdapterText;
        @BindView(R.id.tv_outter_adapter_add)
        TextView tvOutterAdapterAdd;
        @BindView(R.id.tv_outter_adapter_delete)
        TextView tvOutterAdapterDelete;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
